package com.mostromas.ess.system.controller;

import com.mostromas.ess.entities.SysRole;
import com.mostromas.ess.system.req.SysRoleREQ;
import com.mostromas.ess.system.service.ISysRoleService;
import com.mostromas.ess.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 角色信息表-前端控制器
 */
@Api(value = "角色信息管理接口",description = "角色信息管理接口，提供角色信息的增、删、改、查")
@RestController //所有方法都会返回json字符串进行相应
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 根据菜单名称查询菜单列表
     * @param req
     * @return
     */
    @PreAuthorize(("hasAuthority('role:search')"))
    @ApiOperation("根据角色名称查询角色列表接口")
    @PostMapping("/search") //请求路径：/system/role/search
    public Result search(@RequestBody SysRoleREQ req){
        return sysRoleService.queryPage(req);
    }

    @PreAuthorize(("hasAuthority('role:search')"))
    @ApiOperation("查询角色详情接口")
    @ApiImplicitParam(name = "id",value = "角色ID",required = true)
    @GetMapping("/{id}") //请求路径：/system/role/{id}
    public Result view(@PathVariable("id") String id){
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    @PreAuthorize(("hasAuthority('role:search')"))
    @ApiOperation("根据角色id查询拥有的菜单ids接口")
    @ApiImplicitParam(name="id", value="角色ID", required=true)
    @GetMapping("/{id}/menu/ids") // 请求路径 /role/{id}/menu/ids
    public Result findMenuIdsById(@PathVariable("id") String id){
        return sysRoleService.findMenuIdsById(id);
    }

    @PreAuthorize(("hasAuthority('role:edit')"))
    @ApiOperation("修改角色信息接口")
    @PutMapping //put方式调用接口：/system/role
    public Result update(@RequestBody SysRole sysRole){
        sysRole.setUpdateDate(new Date());
        sysRoleService.updateById(sysRole);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('role:add')"))
    @ApiOperation("新增角色信息接口")
    @PostMapping //post方式调用接口：/system/role
    public Result save(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('role:delete')"))
    @ApiOperation("通过角色ID删除菜单信息接口")
    @ApiImplicitParam(name = "id",value = "角色ID",required = true)
    @DeleteMapping("/{id}") //delete方式调用接口：/system/role/{id}
    public Result delete(@PathVariable("id") String id){
        sysRoleService.deleteById(id);
        return Result.ok();
    }

    // allowMultiple=true 表示数组格式的参数,dataType="String" 表示数组中参数的类型
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="角色ID", required=true),
            @ApiImplicitParam(name="menuIds", value="菜单id集合",
                    required=true, allowMultiple=true, dataType = "String")
    })
    @PreAuthorize(("hasAuthority('role:permission')"))
    @ApiOperation("新增角色菜单权限数据接口")
    @PostMapping("/{id}/menu/save")
    public Result saveRoleMenu(@PathVariable("id") String id,@RequestBody List<String> menuIds){
        return sysRoleService.saveRoleMenu(id,menuIds);
    }
}
